package com.fyx.timedTask;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fyx.config.*;
import com.fyx.entity.*;
import com.fyx.mapper.AllFeeMapper;
import com.fyx.mapper.StayUserMapper;
import com.fyx.mapper.SysDictDetailMapper;
import com.fyx.mapper.UserPayBuyDayMapper;
import com.fyx.until.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationOptions;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 累计充值活跃用户
 */
@Slf4j
@Component(value="AllFeeTask")
public class AllFeeTask {
    @Autowired
    private MongoTemplate mongoTemplate;

    @Resource(name = SecondaryMongoConfig.MONGO_TEMPLATE)
    private MongoTemplate secondMongoTemplate;

    @Resource(name = FiveMongoConfig.MONGO_TEMPLATE)
    private MongoTemplate fiveMongoTemplate;

    @Resource(name = SixthMongoConfig.MONGO_TEMPLATE)
    private MongoTemplate sixthMongoTemplate;

    @Resource(name = NinethMongoConfig.MONGO_TEMPLATE)
    private MongoTemplate ninethMongoTemplate;

    @Resource(name = TenthMongoConfig.MONGO_TEMPLATE)
    private MongoTemplate tenthMongoTemplate;

    @Autowired
    private AllFeeMapper allFeeMapper;

    @Autowired
    private StayUserMapper stayUserMapper;

    @Autowired
    private UserPayBuyDayMapper userPayBuyDayMapper;

    public void run(){

      //  record();
        log.info("累计充值活跃用户：执行完毕=======================");
        record1();
        log.info("用户留存：执行完毕=======================");
    }

    public void record(){
        List<AllFee> list = new ArrayList<>();
        long time = DateUtil.getDayBegin().getTime()/1000;
        long nowTime=new Date().getTime()/1000;
        if(time==nowTime){
            time=DateUtil.getBeginDayOfYesterday().getTime()/1000;
        }
        AllFee allFee = new AllFee();
        allFee.setTime((int) time);
        allFee.setAllFee("1-6");
        allFee.setRechargeNum((int) mongoTemplate.count((new Query().addCriteria(Criteria.where("all_fee").gte(1).lt(6))), UserEntity.class));

        allFee.setActiveNum((int) mongoTemplate.count((new Query().addCriteria(Criteria.where("all_fee").gte(1).lt(6).and("last_time").gte(time))), UserEntity.class));
        list.add(allFee);

        allFee = new AllFee();
        allFee.setTime((int) time);
        allFee.setAllFee("6-30");
        allFee.setRechargeNum((int) mongoTemplate.count((new Query().addCriteria(Criteria.where("all_fee").gte(6).lt(30))), UserEntity.class));
        allFee.setActiveNum((int) mongoTemplate.count((new Query().addCriteria(Criteria.where("all_fee").gte(6).lt(30).and("last_time").gte(time))), UserEntity.class));
        list.add(allFee);

        allFee = new AllFee();
        allFee.setTime((int) time);
        allFee.setAllFee("30-100");
        allFee.setRechargeNum((int) mongoTemplate.count((new Query().addCriteria(Criteria.where("all_fee").gte(30).lt(100))), UserEntity.class));
        allFee.setActiveNum((int) mongoTemplate.count((new Query().addCriteria(Criteria.where("all_fee").gte(30).lt(100).and("last_time").gte(time))), UserEntity.class));
        list.add(allFee);

        allFee = new AllFee();
        allFee.setTime((int) time);
        allFee.setAllFee("100-500");
        allFee.setRechargeNum((int) mongoTemplate.count((new Query().addCriteria(Criteria.where("all_fee").gte(100).lt(500))), UserEntity.class));
        allFee.setActiveNum((int) mongoTemplate.count((new Query().addCriteria(Criteria.where("all_fee").gte(100).lt(500).and("last_time").gte(time))), UserEntity.class));
        list.add(allFee);

        allFee = new AllFee();
        allFee.setTime((int) time);
        allFee.setAllFee("500-1000");
        allFee.setRechargeNum((int) mongoTemplate.count((new Query().addCriteria(Criteria.where("all_fee").gte(500).lt(1000))), UserEntity.class));
        allFee.setActiveNum((int) mongoTemplate.count((new Query().addCriteria(Criteria.where("all_fee").gte(500).lt(1000).and("last_time").gte(time))), UserEntity.class));
        list.add(allFee);

        allFee = new AllFee();
        allFee.setTime((int) time);
        allFee.setAllFee("1000-2000");
        allFee.setRechargeNum((int) mongoTemplate.count((new Query().addCriteria(Criteria.where("all_fee").gte(1000).lt(2000))), UserEntity.class));
        allFee.setActiveNum((int) mongoTemplate.count((new Query().addCriteria(Criteria.where("all_fee").gte(1000).lt(2000).and("last_time").gte(time))), UserEntity.class));
        list.add(allFee);

        allFee = new AllFee();
        allFee.setTime((int) time);
        allFee.setAllFee("2000-5000");
        allFee.setRechargeNum((int) mongoTemplate.count((new Query().addCriteria(Criteria.where("all_fee").gte(2000).lt(5000))), UserEntity.class));
        allFee.setActiveNum((int) mongoTemplate.count((new Query().addCriteria(Criteria.where("all_fee").gte(2000).lt(5000).and("last_time").gte(time))), UserEntity.class));
        list.add(allFee);

        allFee = new AllFee();
        allFee.setTime((int) time);
        allFee.setAllFee("5000");
        allFee.setRechargeNum((int) mongoTemplate.count((new Query().addCriteria(Criteria.where("all_fee").gte(5000))), UserEntity.class));
        allFee.setActiveNum((int) mongoTemplate.count((new Query().addCriteria(Criteria.where("all_fee").gte(5000).and("last_time").gte(time))), UserEntity.class));
        list.add(allFee);


        QueryWrapper<AllFee> wrapper =new QueryWrapper<>();
        wrapper.ge("time",time);
        allFeeMapper.delete(wrapper);
        allFeeMapper.insertList(list);
    }

    private Integer getNewUserValue(Long beginTime,Long endTime,SysDictDetail sysDictDetail){
        Criteria criteria = new Criteria();
        criteria.and("reg_time").gte(beginTime).lte(endTime);
        if(sysDictDetail.getValue().equals("total")||sysDictDetail.getValue().equals("totalcl")){}else  {
            if (sysDictDetail.getDictSort()!=null&&sysDictDetail.getDictSort() == 987) {
                if (sysDictDetail.getValue().equals("h5hzmjxl_qq") || sysDictDetail.getValue().equals("h5hzmjxl_wechat")||sysDictDetail.getValue().equals("h5gdmjxl_wechat")||sysDictDetail.getValue().equals("h5hcmjxl_wechat")) {
                    criteria.and("channel").is(sysDictDetail.getValue());
                    criteria.and("invite.channel").is("");
                } else {
                    criteria.and("invite.channel").is(sysDictDetail.getValue());
                }
            } else if (sysDictDetail.getDictSort() == 900||sysDictDetail.getDictSort() == 910||sysDictDetail.getDictSort() == 920) {
                criteria.and("channel").regex(sysDictDetail.getValue().split("_")[0]);
            } else {
                criteria.and("channel").is(sysDictDetail.getValue());
            }
        }
        Aggregation aggregation = Aggregation.newAggregation(Aggregation.match(criteria),
                Aggregation.group().count().as("v1"));
        AggregationResults<StaticeEntity> ar ;
        if(sysDictDetail.getValue().equals("totalcl")||sysDictDetail.getValue().indexOf("cl_")>0){
            ar= fiveMongoTemplate.aggregate(aggregation, "user", StaticeEntity.class);
        }else if(sysDictDetail.getValue().equals("totalapp")||(sysDictDetail.getValue().indexOf("gdmj")==0&&sysDictDetail.getDictSort()!=900&&sysDictDetail.getDictSort()!=910)){
            ar= ninethMongoTemplate.aggregate(aggregation, "user", StaticeEntity.class);
        }else {
            ar = mongoTemplate.aggregate(aggregation, "user", StaticeEntity.class);
        }
        List<StaticeEntity> list = ar.getMappedResults();
        int count = 0;
        if (list.size() > 0) {
            count = list.get(0).getV1();
        }
        return count;
    }

    private Long getValue(Long beginTime,Long endTime,Integer evid,SysDictDetail sysDictDetail,String tableName,String row){
        Criteria criteria = new Criteria();
        if(sysDictDetail.getValue().equals("total")||sysDictDetail.getValue().equals("totalcl")){}else {
            if(sysDictDetail.getDictSort()==987){
                if(sysDictDetail.getValue().equals("h5hzmjxl_qq")||sysDictDetail.getValue().equals("h5hzmjxl_wechat")||sysDictDetail.getValue().equals("h5gdmjxl_wechat")||sysDictDetail.getValue().equals("h5hcmjxl_wechat")){
                    criteria.and("channel").is(sysDictDetail.getValue());
                    criteria.orOperator(Criteria.where("invite_channel").exists(false),Criteria.where("invite_channel").is(""));
                }else {//
                    criteria.and("invite_channel").is(sysDictDetail.getValue());
                }
            }else if(sysDictDetail.getDictSort() == 900||sysDictDetail.getDictSort() == 910){
                criteria.and("channel").regex(sysDictDetail.getValue().split("_")[0]);
            }else {
                criteria.and("channel").is(sysDictDetail.getValue());
            }
        }
        criteria.and("time").gte(beginTime).lte(endTime);
        criteria.and("evid").is(evid);
        AggregationOptions options = new AggregationOptions.Builder().allowDiskUse(true).build();
        Aggregation aggregation = Aggregation.newAggregation(
                Aggregation.match(criteria),
                Aggregation.group(row).count().as("num"),
                Aggregation.group(row).count().as("v5")
        ).withOptions(options);
        AggregationResults<StaticeEntity> ar ;
        if(sysDictDetail.getValue().equals("totalcl")||sysDictDetail.getValue().indexOf("cl_")>0){
            ar= sixthMongoTemplate.aggregate(aggregation, tableName, StaticeEntity.class);
        }else {
            ar = secondMongoTemplate.aggregate(aggregation, tableName, StaticeEntity.class);
        }
        List<StaticeEntity> list = ar.getMappedResults();
        if(list.size()>0){
          return list.get(0).getV5();
        }
        return 0l;
    }

    @Autowired
    private SysDictDetailMapper sysDictDetailMapper;
    private void record1(){

        long beginTime = DateUtil.getDayBegin().getTime()/1000;
        long nowTime=new Date().getTime()/1000;
        String tableName=DateUtil.timeStamp2Date(beginTime+"","yyyyMM");
        if(beginTime==nowTime){
            beginTime=DateUtil.getBeginDayOfYesterday().getTime()/1000;
            if(!DateUtil.timeStamp2Date(beginTime+"","yyyyMM").equals(DateUtil.timeStamp2Date(nowTime+"","yyyyMM"))){
                tableName =DateUtil.timeStamp2Date(beginTime+"","yyyyMM");
            }
        }
        if(nowTime<(beginTime+3600)){  //15分钟内
            beginTime =  DateUtil.getBeginDayOfYesterday().getTime()/1000;
            tableName =DateUtil.timeStamp2Date(beginTime+"","yyyyMM");
        }
        String loginTabel="login_rec_"+tableName;
        tableName="collect_data_"+tableName;
      //  beginTime=1653408000;
        long endTime =beginTime+86400;
        StayUserEntity stayUserEntity = new StayUserEntity();
        QueryWrapper<SysDictDetail> wrapper1=new QueryWrapper<>();
        wrapper1.eq("dict_id",16);
        wrapper1.le("dict_sort",987);
        wrapper1.orderByAsc("dict_sort");
        List<SysDictDetail> list1=sysDictDetailMapper.selectList(wrapper1);
        if(list1.size()>0){
            SysDictDetail sysDictDetail1=new SysDictDetail();
            sysDictDetail1.setValue("total");
            sysDictDetail1.setDictSort(0);
            list1.add(sysDictDetail1);
            sysDictDetail1=new SysDictDetail();
            sysDictDetail1.setValue("totalcl");
            sysDictDetail1.setDictSort(0);
            list1.add(sysDictDetail1);
            sysDictDetail1=new SysDictDetail();
            sysDictDetail1.setValue("totalapp");
            sysDictDetail1.setDictSort(0);
            list1.add(sysDictDetail1);
            sysDictDetail1=new SysDictDetail();
            sysDictDetail1.setValue("h5hzmjxl_wechat");
            sysDictDetail1.setDictSort(987);
            list1.add(sysDictDetail1);
            sysDictDetail1=new SysDictDetail();
            sysDictDetail1.setValue("h5hzmjxl_qq");
            sysDictDetail1.setDictSort(987);
            list1.add(sysDictDetail1);
            sysDictDetail1=new SysDictDetail();
            sysDictDetail1.setValue("h5gdmjxl_wechat");
            sysDictDetail1.setDictSort(987);
            list1.add(sysDictDetail1);
            sysDictDetail1=new SysDictDetail();
            sysDictDetail1.setValue("h5hcmjxl_wechat");
            sysDictDetail1.setDictSort(987);
            list1.add(sysDictDetail1);
            QueryWrapper<StayUserEntity> wrapper2=new QueryWrapper<>();
            wrapper2.eq("time",beginTime);
            stayUserMapper.delete(wrapper2);
            System.out.println(list1);
            for (SysDictDetail sysDictDetail:list1) {
                stayUserEntity = new StayUserEntity();
                stayUserEntity.setChannel(sysDictDetail.getValue());
                stayUserEntity.setTime(beginTime);
                if(sysDictDetail.getDictSort()==0){
                    stayUserEntity.setTheFather(null);
                }else {
                    if(sysDictDetail.getDictSort()!=null&&sysDictDetail.getDictSort()==987) {
                        if(sysDictDetail.getValue().equals("qq_share")||sysDictDetail.getValue().equals("h5hzmjxl_qq")){
                            stayUserEntity.setTheFather("h5hzmjxl_qq");
                        }else if(sysDictDetail.getValue().indexOf("gdmj")>-1){
                            stayUserEntity.setTheFather("h5gdmjxl_wechat");
                        }else if(sysDictDetail.getValue().indexOf("hcmj")>-1){
                            stayUserEntity.setTheFather("h5hcmjxl_wechat");
                        }else {
                            stayUserEntity.setTheFather("h5hzmjxl_wechat");
                        }
                    }else if(sysDictDetail.getDictSort()==900){
                        stayUserEntity.setTheFather("total");
                    }else if(sysDictDetail.getDictSort()==910){
                        stayUserEntity.setTheFather("totalcl");
                    }else if(sysDictDetail.getDictSort()==920){
                        stayUserEntity.setTheFather("totalapp");
                    }else {
                        if(sysDictDetail.getValue().equals("h5zhsmjcl_wechat")){
                            stayUserEntity.setTheFather("zsmjxl_total");
                        }else if(sysDictDetail.getValue().indexOf("gdmj")==0){
                            stayUserEntity.setTheFather(sysDictDetail.getValue().split("_")[0] + "_app_total");
                        }else {
                            stayUserEntity.setTheFather(sysDictDetail.getValue().split("_")[0].replace("h5", "") + "_total");
                        }
                    }
                }
                //新增用户数

                stayUserEntity.setNewUserCount(getNewUserValue(beginTime,endTime,sysDictDetail));

                stayUserEntity.setEnterLoginScene(getValue(beginTime,endTime,1000000003,sysDictDetail,tableName,"dev"));

                stayUserEntity.setClickLogin(getValue(beginTime,endTime,1000000004,sysDictDetail,tableName,"dev"));

                stayUserEntity.setEnterMainScene(getValue(beginTime,endTime,1000000004,sysDictDetail,tableName,"numid"));

                //新增付费用户数
               Criteria criteria = new Criteria();
                criteria.and("reg_time").gte(beginTime).lte(endTime);
                if(stayUserEntity.getChannel().equals("total")||stayUserEntity.getChannel().equals("totalcl")){}
                else{
                    if(sysDictDetail.getDictSort()==987){
                        if(sysDictDetail.getValue().equals("h5hzmjxl_qq")||sysDictDetail.getValue().equals("h5hzmjxl_wechat")||stayUserEntity.getChannel().equals("h5gdmjxl_wechat")||stayUserEntity.getChannel().equals("h5hcmjxl_wechat")){
                            criteria.and("channel").is(stayUserEntity.getChannel());
                            criteria.and("invite.channel").is("");
                        }else {
                            criteria.and("invite.channel").is(stayUserEntity.getChannel());
                        }
                    }else if(sysDictDetail.getDictSort()==900||sysDictDetail.getDictSort()==910){
                        criteria.and("channel").regex(sysDictDetail.getValue().split("_")[0]);
                    }else {
                        criteria.and("channel").is(stayUserEntity.getChannel());
                    }
                }
                criteria.and("all_fee").gt(0);
                Query query1=new Query();
                query1.addCriteria(criteria);
               Aggregation aggregation = Aggregation.newAggregation(Aggregation.match(criteria),
                        Aggregation.group().count().as("v1"));
               AggregationResults<StaticeEntity> ar ;
                if(stayUserEntity.getChannel().equals("totalcl")||stayUserEntity.getChannel().indexOf("cl_")>0){
                    ar= fiveMongoTemplate.aggregate(aggregation, "user", StaticeEntity.class);
                }   else{
                    ar= mongoTemplate.aggregate(aggregation, "user", StaticeEntity.class);

                }
               List<StaticeEntity> list = ar.getMappedResults();
               Integer count = 0;
                if (list.size() > 0) {
                    StaticeEntity statisticsEntity = list.get(0);
                    count = statisticsEntity.getV1();
                }
                stayUserEntity.setNewPayUserCount(count);
                Query query=new Query();
                query.addCriteria(criteria);
                List<UserEntity> userList ;
                if(stayUserEntity.getChannel().equals("totalcl")||stayUserEntity.getChannel().indexOf("cl_")>0){
                    userList= fiveMongoTemplate.find(query,UserEntity.class);
                }   else{
                    userList= mongoTemplate.find(query,UserEntity.class);
                }
                if(userList.size()>0){
                    for(UserEntity user:userList){
                        QueryWrapper<UserPayBuyDay> wrapper=new QueryWrapper<>();
                        wrapper.eq("time",beginTime);
                        wrapper.eq("userid",user.getUserid());
                        wrapper.eq("channel",stayUserEntity.getChannel());
                        wrapper.isNull("flag");
                        UserPayBuyDay userPayBuyDay= userPayBuyDayMapper.selectOne(wrapper);
                        UserPayBuyDay userPayBuyDay1=new UserPayBuyDay();
                        userPayBuyDay1.setTime(beginTime);
                        userPayBuyDay1.setChannel(stayUserEntity.getChannel());
                        userPayBuyDay1.setUserid(user.getUserid());
                        if(userPayBuyDay==null){
                            userPayBuyDayMapper.insert(userPayBuyDay1);
                        }
                    }
                }

                //1天留存用户数
                getDayStay( beginTime-86400, beginTime, beginTime, endTime,sysDictDetail,1);

                //2天留存用户数
                getDayStay( beginTime-86400*2, beginTime-86400, beginTime, endTime,sysDictDetail,2);

                //3天留存用户数
                getDayStay( beginTime-86400*3, beginTime-86400*2, beginTime, endTime,sysDictDetail,3);

                //4天留存用户数
                getDayStay( beginTime-86400*4, beginTime-86400*3, beginTime, endTime,sysDictDetail,4);

                //5天留存用户数
                getDayStay( beginTime-86400*5, beginTime-86400*4, beginTime, endTime,sysDictDetail,5);

                //6天留存用户数
                getDayStay( beginTime-86400*6, beginTime-86400*5, beginTime, endTime,sysDictDetail,6);

                //7天留存用户数
                getDayStay( beginTime-86400*7, beginTime-86400*6, beginTime, endTime,sysDictDetail,7);

                //14天留存用户数
                getDayStay( beginTime-86400*14, beginTime-86400*13, beginTime, endTime,sysDictDetail,14);

                //30天留存用户数
                getDayStay( beginTime-86400*30, beginTime-86400*29, beginTime, endTime,sysDictDetail,30);

                //60天留存用户数
                getDayStay( beginTime-86400*60, beginTime-86400*59, beginTime, endTime,sysDictDetail,60);

                stayUserMapper.insert(stayUserEntity);
            }
        }

    }

    private void getDayStay(Long beginTime,Long endTime,Long bTime,Long eTime,SysDictDetail sysDictDetail,Integer days){
        StayUserEntity stayUserEntity=new StayUserEntity();
        Criteria criteria = new Criteria();
        stayUserEntity.setChannel(sysDictDetail.getValue());
        criteria.and("reg_time").gte(beginTime).lte(endTime);
        if(sysDictDetail.getValue().equals("total")||sysDictDetail.getValue().equals("totalcl")){}else {
            if(sysDictDetail.getDictSort()==987){
                if(sysDictDetail.getValue().equals("h5hzmjxl_qq")||sysDictDetail.getValue().equals("h5hzmjxl_wechat")||sysDictDetail.getValue().equals("h5gdmjxl_wechat")||sysDictDetail.getValue().equals("h5hcmjxl_wechat")){
                    criteria.and("channel").is(sysDictDetail.getValue());
                    criteria.and("invite.channel").is("");
                }else {
                    criteria.and("invite.channel").is(sysDictDetail.getValue());
                }
            }else if(sysDictDetail.getDictSort()==900||sysDictDetail.getDictSort()==910){
                criteria.and("channel").regex(sysDictDetail.getValue().split("_")[0]);
            }else {
                criteria.and("channel").is(stayUserEntity.getChannel());

            }
        }
        criteria.and("last_time").gte(bTime).lte(eTime);
        Aggregation aggregation = Aggregation.newAggregation(Aggregation.match(criteria),
                Aggregation.group().count().as("v1"));
        AggregationResults<StaticeEntity> ar ;
        if(sysDictDetail.getValue().equals("totalcl")||sysDictDetail.getValue().indexOf("cl_")>0){
            ar= fiveMongoTemplate.aggregate(aggregation, "user", StaticeEntity.class);
        }else {
            ar = mongoTemplate.aggregate(aggregation, "user", StaticeEntity.class);
        }
        List<StaticeEntity> list = ar.getMappedResults();
        if (list.size() > 0) {
            if(days==1){
                stayUserEntity.setOneDayNum(list.get(0).getV1());
            }else if(days==2){
                stayUserEntity.setTwoDayNum(list.get(0).getV1());
            }else if(days==3){
                stayUserEntity.setThreeDayNum(list.get(0).getV1());
            }else if(days==4){
                stayUserEntity.setFourDayNum(list.get(0).getV1());
            }else if(days==5){
                stayUserEntity.setFiveDayNum(list.get(0).getV1());
            }else if(days==6){
                stayUserEntity.setSixDayNum(list.get(0).getV1());
            }else if(days==7){
                stayUserEntity.setSevenDayNum(list.get(0).getV1());
            }else if(days==14){
                stayUserEntity.setFourteenDayNum(list.get(0).getV1());
            }else if(days==30){
                stayUserEntity.setThirtyDayNum(list.get(0).getV1());
            }else if(days==60){
                stayUserEntity.setSixtyDayNum(list.get(0).getV1());
            }

        }

        QueryWrapper<UserPayBuyDay> wrapper5=new QueryWrapper<>();
        wrapper5.eq("time",beginTime);
        wrapper5.eq("channel",sysDictDetail.getValue());
        wrapper5.isNull("flag");
        List<UserPayBuyDay> userPayBuyDayList=userPayBuyDayMapper.selectList(wrapper5);
        String[] ids = new String[0];
        if(userPayBuyDayList.size()>0){
            ids = new String[userPayBuyDayList.size()];
            for(int i=0;i<userPayBuyDayList.size();i++){
                ids[i]=userPayBuyDayList.get(i).getUserid();
            }
        }
        criteria = new Criteria();
        criteria.and("id").in(ids);
        if(sysDictDetail.getValue().equals("total")||sysDictDetail.getValue().equals("totalcl")){}else{
            if(sysDictDetail.getDictSort()==987){
                if(sysDictDetail.getValue().equals("h5hzmjxl_qq")||sysDictDetail.getValue().equals("h5hzmjxl_wechat")||sysDictDetail.getValue().equals("h5gdmjxl_wechat")||sysDictDetail.getValue().equals("h5hcmjxl_wechat")){
                    criteria.and("channel").is(sysDictDetail.getValue());
                    criteria.and("invite.channel").is("");
                }else {
                    criteria.and("invite.channel").is(sysDictDetail.getValue());
                }
            }else if(sysDictDetail.getDictSort()==900||sysDictDetail.getDictSort()==910){
                criteria.and("channel").regex(sysDictDetail.getValue().split("_")[0]);
            }else {
                criteria.and("channel").is(sysDictDetail.getValue());
            }
        }
        criteria.and("reg_time").gte(beginTime ).lte(endTime);
        criteria.and("last_time").gte(bTime).lte(eTime);
        criteria.and("all_fee").gt(0);
        aggregation = Aggregation.newAggregation(Aggregation.match(criteria),
                Aggregation.group().count().as("v1"));
        if(sysDictDetail.getValue().equals("totalcl")||sysDictDetail.getValue().indexOf("cl_")>0){
            ar= fiveMongoTemplate.aggregate(aggregation, "user", StaticeEntity.class);
        }else {
            ar = mongoTemplate.aggregate(aggregation, "user", StaticeEntity.class);
        }
        list = ar.getMappedResults();
        if (list.size() > 0) {
            if(days==1){
                stayUserEntity.setOneDayPayNum(list.get(0).getV1());
            }else if(days==2){
                stayUserEntity.setTwoDayPayNum(list.get(0).getV1());
            }else if(days==3){
                stayUserEntity.setThreeDayPayNum(list.get(0).getV1());
            }else if(days==4){
                stayUserEntity.setFourDayPayNum(list.get(0).getV1());
            }else if(days==5){
                stayUserEntity.setFiveDayPayNum(list.get(0).getV1());
            }else if(days==6){
                stayUserEntity.setSixDayPayNum(list.get(0).getV1());
            }else if(days==7){
                stayUserEntity.setSevenDayPayNum(list.get(0).getV1());
            }else if(days==14){
                stayUserEntity.setFourteenDayPayNum(list.get(0).getV1());
            }else if(days==30){
                stayUserEntity.setThirtyDayPayNum(list.get(0).getV1());
            }else if(days==60){
                stayUserEntity.setSixtyDayPayNum(list.get(0).getV1());
            }
        }
        QueryWrapper<StayUserEntity> wrapper=new QueryWrapper<>();
        wrapper.eq("time", beginTime);
        wrapper.eq("channel",stayUserEntity.getChannel());
        if(sysDictDetail.getDictSort()!=null&&sysDictDetail.getDictSort()==987) {
            if(sysDictDetail.getValue().equals("h5hzmjxl_qq")||sysDictDetail.getValue().equals("h5hzmjxl_wechat")||stayUserEntity.getChannel().equals("h5gdmjxl_wechat")||stayUserEntity.getChannel().equals("h5hcmjxl_wechat")){
                wrapper.eq("the_father", stayUserEntity.getChannel());
            }
        }else if(sysDictDetail.getDictSort()!=null&&sysDictDetail.getDictSort()==900){
            wrapper.eq("the_father", "total");
        }else if(sysDictDetail.getDictSort()!=null&&sysDictDetail.getDictSort()==910){
            wrapper.eq("the_father", "totalcl");
        }else if(sysDictDetail.getDictSort()!=null&&sysDictDetail.getDictSort()>0){
            wrapper.like("the_father", "total");
        }
        stayUserMapper.update(stayUserEntity, wrapper);
    }

}
